package com.leo.foodmaster.admin.system.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leo.foodmaster.admin.system.dto.UserDTO;
import com.leo.foodmaster.admin.system.entity.Role;
import com.leo.foodmaster.admin.system.entity.SysUser;
import com.leo.foodmaster.admin.system.service.IRoleService;
import com.leo.foodmaster.admin.system.service.IUserRoleService;
import com.leo.foodmaster.admin.system.service.IUserService;
import com.leo.foodmaster.commons.entity.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author Leo
 * @since 2021-06-17
 */
@RestController
@AllArgsConstructor
@RequestMapping("/system/user")
@Api(tags = "用户信息管理")
public class UserController {
    private final IUserService userService;
    private final IUserRoleService userRoleService;
    private final IRoleService roleService;

    @GetMapping("list")
    @ApiOperation("分页条件查询用户列表")
    public Result<IPage<SysUser>> getUserPageList(
            @ApiParam(value = "当前页，从1开始", required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "每页记录数", required = true) @RequestParam Integer pageSize,
            @ApiParam(value = "机构Id") @RequestParam(required = false) Long organId,
            @ApiParam(value = "部门Id") @RequestParam(required = false) Long deptId,
            @ApiParam(value = "用户账号") @RequestParam(required = false) String account,
            @ApiParam(value = "手机号码") @RequestParam(required = false) String phone,
            @ApiParam(value = "用户状态") @RequestParam(required = false) Integer status,
            @ApiParam(value = "开始时间") @RequestParam(required = false) String beginTime,
            @ApiParam(value = "结束时间") @RequestParam(required = false) String endTime ) {
        Page<SysUser> objectPage = new Page<>(pageNum, pageSize);
        IPage<SysUser> page = userService.getUserPageList(objectPage,organId, deptId,
                account, phone, status, beginTime, endTime);
        return Result.success(page);
    }

    @PostMapping
    @ApiOperation("新增用户")
    public Result<Object> addUser(@RequestBody UserDTO userDto) {
        return userService.addUser(userDto);
    }

    @PutMapping
    @ApiOperation("修改用户")
    public Result<Object> updateUser(@RequestBody UserDTO userDto) {
        return userService.updateUser(userDto);
    }

    @PutMapping("/resetPwd")
    @ApiOperation("重置用户密码")
    public Result<Object> resetPwd(
            @ApiParam("用户Id") @RequestParam Long userId,
            @ApiParam("新密码") @RequestParam String password) {
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String encodePwd = encoder.encode(password);
        boolean update = userService.update(Wrappers.lambdaUpdate(SysUser.class)
                .set(SysUser::getPassword, encodePwd)
                .eq(SysUser::getId, userId));
        return Result.success(update);
    }

    @PutMapping("/updateStatus")
    @ApiOperation("修改用户状态")
    public Result<Object> updateStatus(@RequestBody UserDTO userDTO) {
        SysUser sysUser = new SysUser();
        BeanUtil.copyProperties(userDTO, sysUser);
        return userService.updateStatus(sysUser);
    }

    @DeleteMapping("/{userIds}")
    @ApiOperation("删除用户")
    public Result<Object> deleteUser(@ApiParam("用户Id") @PathVariable String userIds) {
        return userService.deleteUser(userIds);
    }


    @GetMapping("/roleIds/{userId}")
    @ApiOperation("根据用户Id查询角色Id列表")
    public Result<List<Long>> getRoleIdsByUserId(@ApiParam("用户Id") @PathVariable Long userId) {
        List<Long> roleIds = userRoleService.getRoleIdsByUserId(userId);
        return Result.success(roleIds);
    }

    @GetMapping("/roleList/{organId}")
    @ApiOperation("根据机构Id查询角色Id列表")
    public Result<List<Role>> getRoleListByOrganId(@ApiParam("公司Id") @PathVariable Long organId) {
        List<Role> roleList = roleService.list(Wrappers.lambdaUpdate(Role.class)
                .eq(Role::getOrganId, organId));
        return Result.success(roleList);
    }

}

